/**
 * 
 */
package gui.text;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.text.JTextComponent;

/**
 * @author Joshua Lockerman
 *
 */
public class FocusUpTextKeyListener implements KeyListener {
    
    private final JTextComponent parent;
    private final JTextComponent defaultEditor;
    
    /**
     * 
     */
    public FocusUpTextKeyListener(JTextComponent editorToLeave, JTextComponent parent)
    {
        defaultEditor = editorToLeave;
        this.parent = parent;
    }
    
    /* (non-Javadoc)
     * @see java.awt.event.KeyListener#keyTyped(java.awt.event.KeyEvent)
     */
    @Override
    public void keyTyped(KeyEvent e)
    {
        // Nothing to do.
    }
    
    /* (non-Javadoc)
     * @see java.awt.event.KeyListener#keyPressed(java.awt.event.KeyEvent)
     */
    @Override
    public void keyPressed(KeyEvent e)
    {
        if(e.getKeyCode() == KeyEvent.VK_LEFT) {
            if(defaultEditor.getCaret().getDot() == 0) {
                parent.requestFocusInWindow();
            }
        }
        else if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
            int dot = defaultEditor.getCaret().getDot();
            int end = defaultEditor.getDocument().getEndPosition().getOffset() - 1;
            if(dot == end) {
                parent.requestFocusInWindow();
            }
        }
        else if(e.getKeyCode() == KeyEvent.VK_UP) {
            parent.requestFocusInWindow();
        }
        else if(e.getKeyCode() == KeyEvent.VK_DOWN) {
            parent.requestFocusInWindow();
        }
    }
    
    /* (non-Javadoc)
     * @see java.awt.event.KeyListener#keyReleased(java.awt.event.KeyEvent)
     */
    @Override
    public void keyReleased(KeyEvent e)
    {
        // Nothing to do.
    }
    
}
